/*
 * FNISVGEditor - JavaScript - based editor for SVG graphics 
 * Copyright (C) 2008 - 2009 Antti Leppä / Foyt
 * http://www.foyt.fi
 * 
 * License: 
 * 
 * Licensed under GNU Lesser General Public License Version 2.1 or later (the "LGPL") 
 * http://www.gnu.org/licenses/lgpl.html
 */
FNISVGEditorObjectGroupsPlugin = Class.create(FNISVGEditorPlugin, {
  initialize : function($super, editorInstance, pluginURL) {
    $super(editorInstance, pluginURL);

    this._groupButton = new FNIPushButton( {});
    this._groupButton.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/object-group.png");
    this._groupButton.setDisabled(true);
    this._groupButton.addListener('buttonClick', this, this._onGroupButtonClick);
    this._groupButton.setTooltip(SVGLocale.getText("objectgroupsPlugin_groupButtonTooltip"));
    editorInstance.registerToolbarItem('groupObjects', this._groupButton);

    this._ungroupButton = new FNIPushButton( {});
    this._ungroupButton.setImage(this.getPluginURL() + "/gfx/icons/22x22/actions/object-ungroup.png");
    this._ungroupButton.setDisabled(true);
    this._ungroupButton.addListener('buttonClick', this, this._onUngroupButtonClick);
    this._ungroupButton.setTooltip(SVGLocale.getText("objectgroupsPlugin_ungroupButtonTooltip"));
    editorInstance.registerToolbarItem('ungroupObjects', this._ungroupButton);

    editorInstance.getSelectionManager().addListener("selectionChanged", this, this._onSelectionChanged);
  },
  getName : function() {
    return "objectgroups";
  },
  _onSelectionChanged : function(event) {
    var editorInstance = this.getEditorInstance();
    var selectionManager = editorInstance.getSelectionManager();

    var elements = selectionManager.getSelectedElements();
    if ((elements.length == 1) && (elements[0].tagName.toUpperCase() == 'G')) {
      this._groupButton.setDisabled(true);
      this._ungroupButton.setDisabled(false);
    } else if (elements.length > 1) {
      this._groupButton.setDisabled(false);
      this._ungroupButton.setDisabled(true);
    } else {
      this._groupButton.setDisabled(true);
      this._ungroupButton.setDisabled(true);
    }
  },
  _onGroupButtonClick : function(event) {
    var editorInstance = this.getEditorInstance();
    var selectionManager = editorInstance.getSelectionManager();
    var elements = selectionManager.getSelectedElements();
    var groupElement = this.getEditorInstance().createSVGElement('g');
    this.getEditorInstance().appendSVGElement(groupElement);
    for ( var i = 0; i < elements.length; i++) {
      this.getEditorInstance().stopSVGElementObserving(elements[i]);
      groupElement.appendChild(elements[i]);
    }

    selectionManager.clearElementSelection();
    selectionManager.selectElement(groupElement);

  },
  _onUngroupButtonClick : function(event) {
    var editorInstance = this.getEditorInstance();
    var selectionManager = editorInstance.getSelectionManager();

    var groupElement = selectionManager.getSelectedElements()[0];
    selectionManager.clearElementSelection();
    if ((!groupElement) && (groupElement.tagName.toUpperCase() == 'G')) {
      var parentNode = groupElement.parentNode;

      while (groupElement.childNodes.length > 0) {
        var child = groupElement.firstChild;
        var controller = getSVGElementControllerVault().getElementControllerFor(child);
        var bbox = fi.foyt.svg.svgdom.FNISVGDOMUtils.getScreenBBox(child);
        parentNode.appendChild(child);
        this.getEditorInstance().startSVGElementObserving(child);
        controller.resizeTo(child, bbox.width, bbox.height);
        controller.moveTo(child, bbox.x, bbox.y);
        selectionManager.selectElement(child);
      }

      this.getEditorInstance().removeElement(groupElement);
    }
  }
});

document.fire("svgEditorPluginLoaded:objectgroups", {
  pluginClass : FNISVGEditorObjectGroupsPlugin
});
